package com.yeebo.controller.sys;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yeebo.common.BaseController;
import com.yeebo.enums.ResultCode;
import com.yeebo.exception.ServiceException;
import com.yeebo.interceptor.Access;
import com.yeebo.rpc.admin.SysUserService;
import com.yeebo.utils.ResponseUtils;
import com.yeebo.vo.Result;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.LinkedList;
import java.util.List;

@RestController
@RequestMapping("sysUser/manage")
public class SysUserManageController extends BaseController {

    @Resource
    private SysUserService sysUserService;

    @Access
    @PostMapping(value = "/sysUserList")
    @RequiresPermissions("MENU_2019109102647364")
    @ResponseBody
    public Result sysUserList(@RequestBody JSONObject requestBody,
                              HttpServletRequest request) {
        Integer pageNo = requestBody.getInteger("pageNo");
        Integer pageSize = requestBody.getInteger("pageSize");
        String filterUsername = requestBody.getString("filterUsername");
        return ResponseUtils.success(sysUserService.loadSysUserManageList(pageNo, pageSize, filterUsername));
    }

    @Access
    @PostMapping(value = "/addSysUser")
    @RequiresPermissions("RESOURCE_2019109103244964")
    @ResponseBody
    public Result addSysUser(@RequestBody JSONObject requestBody,
                             HttpServletRequest request) {
        String loginName = requestBody.getString("loginName");
        String username = requestBody.getString("username");
        String phone = requestBody.getString("phone");
        sysUserService.addSysUser(loginName, username, phone);
        return ResponseUtils.success("添加成功");
    }

    @Access
    @PostMapping(value = "/getSysUserById")
    @ResponseBody
    public Result getSysUserById(@RequestBody JSONObject requestBody,
                                 HttpServletRequest request) {
        Long userId = requestBody.getLongValue("userId");
        return ResponseUtils.success(sysUserService.getSysUserInfoById(userId));
    }

    @Access
    @PostMapping(value = "/updateSysUserInfo")
    @RequiresPermissions("RESOURCE_2019109103328964")
    @ResponseBody
    public Result updateSysUserInfo(@RequestBody JSONObject requestBody,
                                    HttpServletRequest request) {
        Long userId = requestBody.getLongValue("userId");
        String username = requestBody.getString("username");
        String phone = requestBody.getString("phone");
        Integer status = requestBody.getInteger("status");
        sysUserService.updateSysUser(userId, username, phone, status);
        return ResponseUtils.success("更新成功");
    }

    @Access
    @PostMapping(value = "/resetSysUserPassword")
    @RequiresPermissions("RESOURCE_2019109103328964")
    @ResponseBody
    public Result resetSysUserPassword(@RequestBody JSONObject requestBody,
                                       HttpServletRequest request) {
        Long userId = requestBody.getLongValue("userId");
        sysUserService.resetSysUserPassword(userId);
        return ResponseUtils.success("重置密码成功");
    }

    @Access
    @PostMapping(value = "/deleteSysUser")
    @RequiresPermissions("RESOURCE_2019109103328964")
    @ResponseBody
    public Result deleteSysUser(@RequestBody JSONObject requestBody,
                                       HttpServletRequest request) {
        Long operateUserId = this.getUserId(request);
        Long userId = requestBody.getLongValue("userId");
        if (operateUserId.equals(userId)) {
            throw new ServiceException(ResultCode.CAN_NOT_DELETE_MYSELF);
        }
        sysUserService.deleteSysUser(userId);
        return ResponseUtils.success("删除用户成功");
    }

    @Access
    @PostMapping(value = "/loadSysUserRolesByUserId")
    @ResponseBody
    public Result loadSysUserRolesByUserId(@RequestBody JSONObject requestBody,
                                           HttpServletRequest request) {
        Long userId = requestBody.getLongValue("userId");
        return ResponseUtils.success(sysUserService.loadSysRoleListByUserId(userId));
    }

    @Access
    @PostMapping(value = "/assignRolesToUser")
    @RequiresPermissions("RESOURCE_2019109103354348")
    @ResponseBody
    public Result assignRolesToUser(@RequestBody JSONObject requestBody,
                                    HttpServletRequest request) {
        Long userId = requestBody.getLongValue("userId");
        String[] roleIds = requestBody.getString("roleIdList").split(",");
        List<Long> targetRoleIds = new LinkedList<>();
        for (String roleIdStr : roleIds) {
            targetRoleIds.add(Long.parseLong(roleIdStr));
        }
        sysUserService.assignRolesToUser(userId, targetRoleIds);
        return ResponseUtils.success("成功分配对应相应角色");
    }

}
